class zcPromise {
  _state = "pending";

  set state(val) {
    this._state = val;
    this.deal();
  }

  deal() {
    if (this.state == "OK" && this.thenFN) {
      this.thenFN(this.value);
    } else if (this.state == "Error" && this.catchFn) {
      this.catchFn(this.reason);
    } else {
      const r = setTimeout(() => {
        this.deal();
        clearTimeout(r);
      });
    }
  }

  get state() {
    return this._state;
  }

  value = undefined;
  reason = undefined;

  resolve = (value) => {
    console.log("This Is Resolve");
    this.value = value;
    this.state = "OK";
  };
  reject = (reason) => {
    console.log("This Is ReJect");
    this.reason = reason;
    this.state = "Error";
  };

  constructor(fn) {
    fn(this.resolve, this.reject);
  }

  then(thenFN) {
    this.thenFN = thenFN;
    return this;
  }

  catch(catchFn) {
    this.catchFn = catchFn;
    return this;
  }
}

new zcPromise((resolve, reject) => {
  const num = (Math.random() * 10).toFixed(2);
  setTimeout(() => {
    num >= 5 ? resolve(num) : reject(num);
  }, 1500);

  // resolve("value");
})
  .then((res) => {
    console.log("then : " + res);
  })
  .catch((eror) => console.log("Catch : " + eror));
